package siga.visao_financeiro;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import siga.banco.conexao;
import siga.banco.unimestre;
import siga.visao.index;
import siga.visao.verErros;

public class MemoImportacao extends javax.swing.JInternalFrame {


    public MemoImportacao() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jbImportar = new javax.swing.JButton();
        jtfDataImport = new javax.swing.JFormattedTextField();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Importação do Dados do Unimestre");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/Business_People_16x16.png"))); // NOI18N
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setLayout(null);

        jLabel1.setText("Data da Ultima Importação:");
        jLabel1.setName("jLabel1"); // NOI18N
        jPanel1.add(jLabel1);
        jLabel1.setBounds(10, 10, 180, 18);

        jbImportar.setText("Importar");
        jbImportar.setName("jbImportar"); // NOI18N
        jbImportar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbImportarActionPerformed(evt);
            }
        });
        jPanel1.add(jbImportar);
        jbImportar.setBounds(210, 30, 100, 20);

        jtfDataImport.setEditable(false);
        try {
            jtfDataImport.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jtfDataImport.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jtfDataImport.setName("jtfDataImport"); // NOI18N
        jPanel1.add(jtfDataImport);
        jtfDataImport.setBounds(10, 30, 190, 28);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 322, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        try {
            conexao.Query("select * from importacao_memorando order by codigo LIMIT 1");
            if (conexao.rs.next()) {
                jtfDataImport.setText(new SimpleDateFormat("dd/MM/yyyy").format(conexao.rs.getDate("data")));
            }else{
                jtfDataImport.setText(index.dataAgoraBR);
            }
        } catch (SQLException ex) {
            verErros.setErro(ex.getMessage());
        }
    }//GEN-LAST:event_formInternalFrameOpened

    private void jbImportarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbImportarActionPerformed
        try {
            unimestre.Conexao();
            String data = new SimpleDateFormat("yyyy-MM-dd").format(jtfDataImport.getText());
            unimestre.Query("select * from diario_aulas where data > \"" + data + " 00:00:00\" and turma like \"5%\" and anosemestre like \"2009%\" and nro_aula=1");
            while (conexao.rs.next()) {
                conexao.Inserir("insert into fin_memorando (cod_emp,cod_matricula,cod_professor,cod_disciplina,dt_requerida,dt_pagamento,valor,status)" +
                        "   values("+index.cod_emp+",\""+unimestre.rs.getString("turma")+"\","+unimestre.rs.getString("cd_professor")+","+unimestre.rs.getString("disciplina")+",now(),now(),0.00,\"PGTO\")");
                
            }
            unimestre.FechaConexao();
        } catch (SQLException ex) {
            verErros.setErro(ex.getMessage());
        }
    }//GEN-LAST:event_jbImportarActionPerformed
   

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton jbImportar;
    private javax.swing.JFormattedTextField jtfDataImport;
    // End of variables declaration//GEN-END:variables

}
